﻿using UnityEngine;
using System.Collections;


public enum ChessType
{
    None = 0,
    Black = 1,
    White = 2,
}
public class BoardModel
{

    public const int WinChessCount = 5;

    ChessType[,] _data = new ChessType[Board.CrossCount, Board.CrossCount];

    public ChessType Get(int x, int y)
    {
        if (x < 0 || x > Board.CrossCount)
        {
            return ChessType.None;
        }

        if (y < 0 || y > Board.CrossCount)
        {
            return ChessType.None;
        }

        return _data[x, y];
    }

    public bool Set(int x, int y,ChessType type)
    {
        if (x < 0 || x > Board.CrossCount)
        {
            return false;
        }

        if (y < 0 || y > Board.CrossCount)
        {
            return false;
        }

        _data[x, y] = type;
        return true;
    }

    /// <summary>
    /// 向垂直方向检索相同棋子的个数
    /// </summary>
    /// <param name="px">被检索棋子的坐标x</param>
    /// <param name="py">被检索棋子的坐标Y</param>
    /// <param name="type">被检索棋子的类别</param>
    /// <returns>相同棋子的个数</returns>
    int CheckVerticalLink(int px, int py, ChessType type)
    {
        int linkCount = 1;
        //向上方检索
        for (int y = py + 1; y <= Board.CrossCount; y++)
        {
            if (Get(px,y)==type)
            {
                linkCount++;
                if (linkCount>=WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        //向下方检索
        for (int y = py - 1; y >= 0; y--)
        {
            if (Get(px, y) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        return linkCount;
    }

    /// <summary>
    /// 向水平方向检索相同类型棋子的个数
    /// </summary>
    /// <param name="px">被检索棋子的坐标x</param>
    /// <param name="py">被检索棋子的坐标y</param>
    /// <param name="type">被检索棋子的类别</param>
    /// <returns>相同棋子的个数</returns>
    int CheckHorizontalLink(int px, int py, ChessType type)
    {
        int linkCount = 1;
        //向右方检索
        for (int x = px + 1; x <= Board.CrossCount; x++)
        {
            if (Get(x, py) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        //向左方检索
        for (int x = px - 1; x >= 0; x--)
        {
            if (Get(x, py) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        return linkCount;
    }

    /// <summary>
    /// 向左上斜角方向检索相同类型棋子的个数
    /// </summary>
    /// <param name="px">被检索棋子的坐标x</param>
    /// <param name="py">被检索棋子的坐标y</param>
    /// <param name="type">被检索棋子的类别</param>
    /// <returns>相同棋子的个数</returns>
    int CheckLeftBevelLink(int px, int py, ChessType type)
    {
        //算上自己
        int linkCount = 1;
        //向左上方检索
        for (int x = px - 1,y = py + 1; x >=0 && y<=Board.CrossCount; x--,y++)
        {
            if (Get(x, y) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        //向右下角方检索
        for (int x = px + 1, y = py - 1; x <= Board.CrossCount && y >= 0; x++, y--)
        {
            if (Get(x, y) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        return linkCount;
    }

    /// <summary>
    /// 向右上斜角方向检索相同类型棋子的个数
    /// </summary>
    /// <param name="px">被检索棋子的坐标x</param>
    /// <param name="py">被检索棋子的坐标y</param>
    /// <param name="type">被检索棋子的类别</param>
    /// <returns>相同棋子的个数</returns>
    int CheckRightBevelLink(int px, int py, ChessType type)
    {
        //算上自己
        int linkCount = 1;
        //向左下方检索
        for (int x = px - 1, y = py - 1; x >= 0 && y >= 0; x--, y--)
        {
            if (Get(x, y) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        //向右上角方检索
        for (int x = px + 1, y = py + 1; x <= Board.CrossCount && y <= Board.CrossCount; x++, y++)
        {
            if (Get(x, y) == type)
            {
                linkCount++;
                if (linkCount >= WinChessCount)
                {
                    return linkCount;
                }
            }
            else
            {
                break;
            }
        }
        return linkCount;
    }

    /// <summary>
    /// 检查定点周围的最大连接情况
    /// </summary>
    /// <param name="px">定点的坐标x</param>
    /// <param name="py">定点的坐标y</param>
    /// <param name="type">定点的棋子类型</param>
    /// <returns></returns>
    public int CheckLink(int px, int py, ChessType type)
    {
        int linkCount = 0;
        linkCount = Mathf.Max(CheckVerticalLink(px, py, type), linkCount);
        linkCount = Mathf.Max(CheckHorizontalLink(px, py, type), linkCount);
        linkCount = Mathf.Max(CheckLeftBevelLink(px, py, type), linkCount);
        linkCount = Mathf.Max(CheckRightBevelLink(px, py, type), linkCount);

        return linkCount;
    }
}
